/**
 * Created by Administrator on 2018/3/1 0001.
 */



$("#addProblem").click(function () {
    showModel("addProblem-div", function () {
        $("#answer").val('');
        $("#question").val('');
        $("#type").val(0);
    })
});

/**
 * 添加
 */
bindClick("button[data-event=submit-addProblem]", function () {

    var types = $("#type").val();
    if (types == 0) {
        hint("请选择分类");
        return;
    }
    $("#select").val(types);

    var price = $("#question").val();
    if (price == "") {
        hint("请填写问题");
        return;
    }

    var answer = UE.getEditor('answerAdd').getContent();
    $("#answer").val(answer);

    var form = $("#addProblemFrom");
    var param = form.formToArray();
    $.post($().AbsUrlParse('/web/app/addProblem'), param, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('添加成功');
            //关闭model方法
            closeModel('addProblem-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});


problemFormatter = function () {
    return [
        '<div><a class="update" href="javascript:void(0)">修改</a>' +
        '&nbsp;&nbsp;' +
        '<a class="remove" href="javascript:void(0)" style="color: red">删除</a></div>'
    ].join('');
};

problemEvent = {
    'click .update': function (e, value, row) {
        showModel("updateProblem-div", function () {
            $("#id").val(row.id);
            $("#problem").val(row.question);
            // $("#answers").val(row.answer);
            $("#typeUpdate").val(row.type);
            answerObtain.setContent(row.answer)
        })
    },
    'click .remove': function (e, value, row) {
        comment.showOperationTips("确定删除吗？", function () {
            $.post($().AbsUrlParse('/web/app/deleteProblem'), {
                id: row.id
            }, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("删除成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    }
};

/**
 * 修改
 */
bindClick("button[data-event=submit-updateProblem]", function () {
    var type = $("#typeUpdate").val();
    if (type == 0) {
        hint("请选择分类");
        return;
    }
    $("#selectUpdate").val(type);

    var answer = UE.getEditor('answerObtain').getContent();
    $("#answerUpdate").text(answer);

    var form = $("#updateProblemFrom");
    var param = form.formToArray();
    $.post($().AbsUrlParse('/web/app/updateProblem'), param, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('修改成功');
            //关闭model方法
            closeModel('updateProblem-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

type = function (value, row, index) {
    if (row.type == 1) {
        return [
            '<div>' + '用户端' + '</div>'
        ].join('');
    } else {
        return [
            '<div>' + '商家端' + '</div>'
        ].join('');
    }
};